package com.unswift.window.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.unswift.QuickApplication;
import com.unswift.constant.ButtonConstant;
import com.unswift.utils.ObjectUtils;
import com.unswift.window.editor.Editor;
import com.unswift.window.extend.Attribute;
import com.unswift.window.factory.ButtonFactory;
import com.unswift.window.factory.FormFactory;
import com.unswift.window.factory.PanelFactory;
import com.unswift.window.factory.ScrollFactory;

@SuppressWarnings("serial")
public class HelpDialog extends QuickDialog{

	private int width;
	
	public HelpDialog(JFrame frame, String title, String type) {
		super(frame, ObjectUtils.init(title, "帮助"), false);
		
		setSize(frame);
		JPanel root = (JPanel)this.getContentPane();
		initContentPane(root);
		initHelpContent(root, type);
		initBottom(root);
		setVisible(true);
	}
	
	private void setSize(JFrame frame) {
		int fwidth=frame.getWidth();
		int fheight=frame.getHeight();
		width=700;
		int height=500;
		int x=(fwidth-width)/2;
		int y=(fheight-height)/2;
		setBounds(x, y, width, height);
	}
	
	private void initContentPane(JPanel root){
		root.setLayout(new BorderLayout());
	}
	
	private void initHelpContent(JPanel root, String type){
		String content=QuickApplication.getDescribe(type);
		Editor helpArea=FormFactory.createFormArea(
			Attribute.create()
			.put(Attribute.ATTRIBUTE_FORM_ACTION, Editor.ACTION_HELP)
			.put(Attribute.ATTRIBUTE_FORM_VALUE, content)
			.put(Attribute.ATTRIBUTE_FORM_MARGIN,  new Insets(5, 5, 5, 5))
			.put(Attribute.ATTRIBUTE_FORM_EDITABLE, false)
			.put(Attribute.ATTRIBUTE_FORM_FOCUS, true)
		);
		JPanel contentPanel=PanelFactory.create(Color.WHITE, BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(187, 187, 187)), new BorderLayout());
		contentPanel.add(helpArea);
		root.add(ScrollFactory.addScroll(contentPanel, null, null), BorderLayout.CENTER);
	}
	
	public void initBottom(JPanel root){
		JPanel panel=PanelFactory.create(null, null, new FlowLayout(FlowLayout.RIGHT));
		panel.add(ButtonFactory.createDialogButton(
			Attribute.create()
			.put(Attribute.ATTRIBUTE_BUTTON_NAME, "关闭")
			.put(Attribute.ATTRIBUTE_BUTTON_ACTION, ButtonConstant.ACTION_DIALOG_CLOSE)
			.put(Attribute.ATTRIBUTE_BUTTON_DEPEND, this)
		));
		root.add(panel, BorderLayout.SOUTH);
	}
}
